package com.weilele.leisure.video_factory.ui.module.common.holder


import android.content.ContentUris
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.view.View
import androidx.core.content.ContextCompat.startActivity
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.weilele.leisure.time.R
import com.weilele.leisure.time.databinding.HolderLocalVideoItemBinding
import com.weilele.leisure.video_factory.utils.MediaAudioInfo
import com.weilele.leisure.video_factory.utils.MediaVideoInfo
import com.weilele.mvvm.activityList
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.mvvmRcvAdapter
import com.weilele.mvvm.app
import com.weilele.mvvm.utils.activity.getActivity
import com.weilele.mvvm.utils.activity.getResString
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.utils.coroutine.switchToThread
import com.weilele.mvvm.utils.getCountTimeByLong
import com.weilele.mvvm.utils.glide.setImageAny
import com.yizisu.playerlibrary.IYzsPlayer
import com.yizisu.playerlibrary.activity.FullScreenVideoActivity
import com.yizisu.playerlibrary.helper.SimplePlayerListener
import kotlinx.coroutines.CoroutineScope


class LocalVideoItemHolder : MvvmHolder<MediaVideoInfo, HolderLocalVideoItemBinding>,
    SimplePlayerListener<IYzsPlayer.Model> {
    companion object {
        fun toAdapter() = mvvmRcvAdapter(LocalVideoItemHolder::class.java)
    }

    init {
        itemView.onClick {
            dataWhenBind?.let { video ->
                FullScreenVideoActivity.start(
                    itemView.getActivity(),
                    FullScreenVideoActivity.VideoInfo(
                        video.width ?: 0,
                        video.height ?: 0,
                        video.title,
                        video.uri.toString(),
                        dataWhenBind?.bookmark ?: 0L
                    ), this
                ) {
                    //安卓11，需要权限,否在无法更新数据库
                    it.data = video.uri
                    it.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                }
            }
        }
    }

    constructor(binding: HolderLocalVideoItemBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    override fun bindData(data: MediaVideoInfo) {
        mBinding.tvName.text = data.title ?: data.displayName
        mBinding.ivCover.setImageAny(data.coverUri)
        mBinding.tvTime.text =
            "${getResString(R.string.time_duration)} ${getCountTimeByLong(data.duration ?: 0L)}"
        val bookmark = data.bookmark
        if (bookmark != null) {
            mBinding.tvDes.text =
                "${getResString(R.string.last_play_duration)} ${getCountTimeByLong(bookmark)}"
        } else {
            mBinding.tvDes.text = null
        }
    }

    override suspend fun onTick(playerModel: IYzsPlayer.Model, scope: CoroutineScope) {
        super.onTick(playerModel, scope)
        dataWhenBind?.bookmark = playerModel.currentDuration
        mBinding.tvDes.text =
            "${getResString(R.string.last_play_duration)} ${getCountTimeByLong(playerModel.currentDuration)}"
        switchToThread {
            dataWhenBind?.uri?.let {
                app.contentResolver.update(
                    it,
                    ContentValues().apply {
                        put(MediaStore.Video.Media.BOOKMARK, playerModel.currentDuration)
                    },
                    null, null
                )
            }
        }
    }
}